package com.slkj.shilixiaoyuanapp.ui.teacher.tool.OutWork;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.example.api.InjectParam;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.data.UserRequest;
import com.slkj.shilixiaoyuanapp.entity.PeopleEntity;
import com.slkj.shilixiaoyuanapp.entity.ToolOutWorkDetailEntity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.ui.common.ChosePeopleActivity;
import com.slkj.shilixiaoyuanapp.ui.common.ChosePeopleActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.util.TimeUtils;
import com.slkj.shilixiaoyuanapp.view.NumberEditText;
import com.slkj.shilixiaoyuanapp.view.PickerViewProvider;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadSuccessAndFailDialog;
import com.tkk.api.RxEventProcessor;
import com.tkk.api.annotation.Recipient;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.slkj.shilixiaoyuanapp.entity.code.RxEventProcessorTag.CHOSE_SPR_FINSH;

@ActivityInfo(layout = R.layout.activity_outwork_wtg, title = "未通过")
public class OutWorkWtgActivity extends BaseActivity {
    @InjectParam
    int id;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.tv_spr)
    TextView tvSpr;
    @BindView(R.id.edit_day)
    EditText editDay;
    @BindView(R.id.edit_where)
    EditText editWhere;
    @BindView(R.id.edit_content)
    NumberEditText editContent;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_now_day_time)
    TextView tvNowDayTime;
    @BindView(R.id.tv_remark)
    TextView tvWtg;
    @InjectParam
    ToolOutWorkDetailEntity data;
    List<Integer> sqrIds = new ArrayList<>();
    private ArrayList<PeopleEntity> peopleEntities;
    public static final String tag = "OutWorkWtgActivity";
    private String startTime, endTime;
    Date date_start = null;
    Date date_end = null;
    @Override
    protected void init() {
        OutWorkWtgActivity__JumpCenter.bind(this);
        RxEventProcessor.get().bind(this);
        tvUserName.setText(UserRequest.getInstance().getUser().getUser().getUName());
        tvNowDayTime.setText(data.getInntime());
        editWhere.setText(data.getSite());
        tvStartTime.setText(data.getStartDate());
        tvEndTime.setText(data.getEndDate());
        editDay.setText(String.valueOf(data.getDuration()));
        editContent.setText(data.getBody());
        tvSpr.setText(ChosePeopleActivity.getPeoplesInfos(data.getAuditor()));
        tvWtg.setText(data.getRefute());
        date_start = TimeUtils.strToDate(data.getStartDate());
        date_end = TimeUtils.strToDate(data.getEndDate());

        startTime = data.getStartDate();
        endTime = data.getEndDate();
        sqrIds = ChosePeopleActivity.getIdList(data.getAuditor());

    }
    @OnClick(R.id.tv_up)
    void upData() {
        String posStr = editWhere.getText().toString();
        if (TextUtils.isEmpty(posStr)) {
            showToast("请输入出差地点");
            return;
        }
        if (TextUtils.isEmpty(startTime)) {
            showToast("请选择开始时间");
            return;
        }
        if (TextUtils.isEmpty(endTime)) {
            showToast("请选择结束时间");
            return;
        }
        String day = editDay.getText().toString();
        if (TextUtils.isEmpty(day)) {
            showToast("请输入出差时长");
            return;
        }
        String context = editContent.getText();
        if (TextUtils.isEmpty(context)) {
            showToast("请输入出差事由");
            return;
        }
        if (sqrIds.size() == 0) {
            showToast("请选择审批人");
            return;
        }


        HttpHeper.get().toolService()
                .addOutWork(posStr,sqrIds.toString(),startTime,endTime,day,context)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<String>(true,this) {
                    @Override
                    public void onCallBackSuccess(String data) {
                        LoadSuccessAndFailDialog.showSuccess(OutWorkWtgActivity.this,data);
                    }
                });
    }
    @Recipient(tag = tag+ CHOSE_SPR_FINSH)
    void onChoseObjFinsh(int code, String info, List<PeopleEntity> peopleEntityList, List<Integer> checkIds) {
        tvSpr.setText(checkIds.size() == 0 ? "" : info);
        sqrIds.clear();
        sqrIds.addAll(checkIds);
        peopleEntities.clear();
        peopleEntities.addAll(peopleEntityList);
    }
    @OnClick(R.id.layout_chose_start_time)
    public void onLayoutChoseStartTimeClicked() {
        PickerViewProvider.getDayTimePickerBefor(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                date_start=date;
                startTime = TimeUtils.dateToStr(date);

                tvStartTime.setText(startTime);
                if (date_end!=null&&date_start!=null){
                    editDay.setText(TimeUtils.getGapCount(date_start,date_end)+"");
                }
            }
        },date_end).show();
    }

    @OnClick(R.id.layout_chose_end_time)
    public void onLayoutChoseEndTimeClicked() {
        PickerViewProvider.getDayTimePickerAfter(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                date_end=date;
                endTime = TimeUtils.dateToStr(date);

                tvEndTime.setText(endTime);
                if (date_end!=null&&date_start!=null){
                    editDay.setText(TimeUtils.getGapCount(date_start,date_end)+"");
                }
            }
        },date_start).show();
    }

    @OnClick(R.id.layout_chose_spr)
    public void onLayoutChoseSprClicked() {
        if (peopleEntities == null) {
            HttpHeper.get().toolService()
                    .getCcSqr()
                    .compose(bindToLifecycle())
                    .compose(getThread())
                    .subscribe(new CommonCallBack<ArrayList<PeopleEntity>>(true, this) {
                        @Override
                        public void onCallBackSuccess(ArrayList<PeopleEntity> data) {
                            peopleEntities = data;
                            ChosePeopleActivity__JumpCenter.builder(OutWorkWtgActivity.this).setTag(tag).setData(peopleEntities).create().go();
                        }
                    });
        }else {
            ChosePeopleActivity__JumpCenter.builder(this).setTag(tag).setData(peopleEntities).create().go();

        }

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxEventProcessor.get().unBind(this);
    }
}
